package com.ipay.wechat.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ipay.redis.JedisExecutor;
import com.ipay.redis.JedisKeyPrefix;
import com.ipay.wechat.component.Wechat;
import com.ipay.wechat.util.HttpUtils;
import com.ipay.wechat.util.PushMessage;

@Controller
public class PushMessageController {
	private Logger logger=LoggerFactory.getLogger(getClass());
	@Autowired
	private Wechat wechat;
	@Autowired
	private JedisExecutor jedies;
	
	
	@RequestMapping("/applyMessage")
	public void applyMessage(HttpServletRequest request,HttpServletResponse response) throws IOException{
		HashMap<String, String> map =new HashMap<String, String>();
		PrintWriter out = response.getWriter();
		String access_token=jedies.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_WECHAT_TOKEN, "access_token");
		access_token = "daoying";
		/*Object openidObj = request.getSession().getAttribute("openid");
		String openid= String.valueOf(openidObj);*/
		map.put("openid", "oejR50lPUQq5eRVL31sSIyEo0Ybg");
		map.put("TEMPLATE1", wechat.getTEMPLATE1());
		map.put("index", "1");
		String msg = PushMessage.createMsgJson(map);
		String result = HttpUtils.requestHttp("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, "post", msg);
		logger.info(result);
		out.write(result);
		out.flush();
		out.close();
	}
}
